package com.dsbridge.bank.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dsbridge.bank.business.CustomerBusinessServiceInterface;
import com.dsbridge.bank.model.Customer;

@Controller
public class CustomerRegistration {

	@Autowired
	CustomerBusinessServiceInterface custSer;

	@RequestMapping(value = "/submited.mvc", method= RequestMethod.POST )
	public @ResponseBody String registration(
			@ModelAttribute("customer") Customer customer) throws Exception {

		custSer.insert(customer);
		ModelAndView modelAndView = new ModelAndView(
				"RegistrationSucessful.jsp");
		modelAndView.addObject("customer", customer);
		return "user added succes fully";

	}

	@RequestMapping(value = "/update.mvc")
	public ModelAndView update(@ModelAttribute("customer") Customer customer)
			throws Exception {

		custSer.update(customer);
		ModelAndView modelAndView = new ModelAndView("updationsucessful.jsp");
		modelAndView.addObject("customer", customer);
		return modelAndView;

	}

	@RequestMapping(value = "/delete.mvc")
	public ModelAndView delete(@ModelAttribute("customer") Customer customer)
			throws Exception {
		Customer custome1;

		custome1 = custSer.delete(customer);
		ModelAndView modelAndView = new ModelAndView("deletionsucessful.jsp");
		modelAndView.addObject("customer", custome1);
		return modelAndView;

	}

	@RequestMapping(value = "/show.mvc")
	public ModelAndView retrieveCustomerbyTaxid(
			@ModelAttribute("customer") Customer customer) throws Exception {

		customer = custSer.retrieveCustomerbyTaxid(customer.getTaxid());
		ModelAndView modelAndView = new ModelAndView("customerdisplay.jsp");
		modelAndView.addObject("customer", customer);
		return modelAndView;

	}

	@RequestMapping(value = "/showallcustomer.mvc")
	public ModelAndView showAll() throws Exception {

		List<Customer> Listcustomer = custSer.showAllCustomer();
		ModelAndView modelAndView = new ModelAndView("Showallcustomers.jsp");
		for (Customer c : Listcustomer)
			System.out.println(c.getCustomername());
		modelAndView.addObject("customer", Listcustomer);
		return modelAndView;

	}

}
